﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ObjectExtensions.cs" company="">
//   
// </copyright>
// <summary>
//   Extension methods for all objects.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Skymate.Extensions
{
    using System;
    using System.Linq;

    /// <summary>
    ///     Extension methods for all objects.
    /// </summary>
    public static class ObjectExtensions
    {
        /// <summary>
        /// Used to simplify and beautify casting an object to a type.
        /// </summary>
        /// <typeparam name="T">
        /// Type to be casted
        /// </typeparam>
        /// <param name="obj">
        /// Object to cast
        /// </param>
        /// <returns>
        /// Casted object
        /// </returns>
        public static T As<T>(this object obj) where T : class
        {
            return (T)obj;
        }

        /// <summary>
        /// Converts given object to a value type using <see cref="Convert.ChangeType(object,System.TypeCode)"/> method.
        /// </summary>
        /// <param name="obj">
        /// Object to be converted
        /// </param>
        /// <typeparam name="T">
        /// Type of the target object
        /// </typeparam>
        /// <returns>
        /// Converted object
        /// </returns>
        public static T To<T>(this object obj) where T : struct
        {
            return (T)Convert.ChangeType(obj, typeof(T));
        }

        /// <summary>
        /// Check if an item is in a list.
        /// </summary>
        /// <param name="item">
        /// Item to check
        /// </param>
        /// <param name="list">
        /// List of items
        /// </param>
        /// <typeparam name="T">
        /// Type of the items
        /// </typeparam>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public static bool IsIn<T>(this T item, params T[] list)
        {
            return list.Contains(item);
        }
    }
}